package org.elkastud.util;

import java.util.LinkedHashMap;
import java.util.Map;
import org.elkastud.model.DNASequence;

/**
 * Tools to make string from result
 * @author Mariusz Jasiński
 */
public class PrintComparator {

    /**
     *
     * @param a first sequence
     * @param b second sequence
     * @param html if true return colored html string
     * @return string contains compare rezult
     */
    public static String compareSequences(DNASequence a, DNASequence b, boolean html) {
        if (colorMap == null && html) {
            initColorMap();
        }
        StringBuilder builder = new StringBuilder();
        if (html)
            builder.append("<html>");
        for (int i=0; i<a.length() && i<b.length(); ++i) {
            if (html) {
                if (a.getAt(i).getNucleobaseCode() == b.getAt(i).getNucleobaseCode()) {
                    builder.append("<font color='green'>");
                    builder.append('|');
                    builder.append("</font>");
                } else {
                    builder.append("&nbsp;");
                }
            } else {
                if (a.getAt(i).getNucleobaseCode() == b.getAt(i).getNucleobaseCode()) {
                    builder.append('|');
                } else {
                    builder.append(' ');
                }
            }
        }
        if (html)
            builder.append("</html>");

        return builder.toString();
    }

    /**
     * Build sequence from string
     * @param seq sequence to build
     * @param html if true build colored html string
     * @return sequence string
     */
    public static String getColoredSeq(DNASequence seq, boolean html) {
        if (colorMap == null && html) {
            initColorMap();
        }
        StringBuilder builder = new StringBuilder();
        if (html)
            builder.append("<html>");
        for (int i=0; i<seq.length(); ++i) {
            char code = Character.toUpperCase(seq.getAt(i).getNucleobaseCode());
            if (colorMap.containsKey(code) && html) {
                builder.append("<font color='");
                builder.append(colorMap.get(code));
                builder.append("'>");
                builder.append(code);
                builder.append("</font>");
            } else {
                builder.append(code);
            }
        }
        if (html)
            builder.append("</html>");
        return builder.toString();
    }

    /**
     * init color map to sequence
     */
    private static void initColorMap() {
        colorMap = new LinkedHashMap<Character, String>();
        colorMap.put('A', "red");
        colorMap.put('T', "blue");
        colorMap.put('C', "black");
        colorMap.put('G', "green");
        colorMap.put('-', "yellow");
    }

    private static Map<Character, String> colorMap;
}
